package com.cool.reggie.config;

import com.cool.reggie.utils.ResultData;
import org.springframework.web.servlet.HttpServletBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;

/**
 * 此处运用了设计模式：模板方法
 *
 * @Author ifredomvip@gmail.com
 * @Date 2022/7/6 14:59
 * @Version 1.0.0
 * @Description
 **/
@WebServlet("/life")
public class MyServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);

        System.out.println("重写servlet,username: " );

        PrintWriter writer = resp.getWriter();
        ResultData resultData = ResultData.ok();
        writer.println(resultData.getMsg());

        // 转发以及传递数据   用于数据与UI分离
        req.getRequestDispatcher("ServletB").forward(req,resp);

        // 重定向
        resp.sendRedirect("target Url地址");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);

        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        System.out.println("重写servlet,username: " + username);
    }
}
